
def get_video_url(video, mirror: str="") -> str:
    if hasattr(video, 'url'): 
        return video.url
    elif hasattr(video, 'videoId'): 
        return mirror + "/watch?v=" + video.videoId
    else: return ""

def get_plugin_by_flag(plugins, flag):
    for plugin in plugins: 
        if plugin.full_flag[2:] == flag: return plugin
    return None

def argstr_to_dict(argstr: str="") -> dict:
    if argstr == "": return {}
    lst = argstr.split("&")
    dct = {}
    for item in lst:
        item_lst    = item.split("=")
        title       = item_lst[0]
        arg         = item_lst[1]
        dct[title]  = arg
    return dct

def get_channels_indexs_by_link_list(channels, link_list):
    index_dict = {}

    for lindex, link in enumerate(link_list):
        for index, item in enumerate(channels):
            if link in item.authorId:
                index_dict[lindex] = index
                break

    return index_dict

def sort_channels_by_link_list(channels, link_list):
    index_dict = get_channels_indexs_by_link_list(channels, link_list)

    sorted_channels = []
    for key, value in index_dict.items():
        sorted_channels.insert(key, channels[value])

    return sorted_channels

def convert_big_nums(num: int=0) -> str:
    cats = {
        'B' : 1000000000,
        'M' : 1000000,
        'K' : 1000,
    }
    
    sub = ''
    for key in cats.keys():
        if num >= cats[key]:
            num = int(num/cats[key])
            sub = key

    return f"{num}{sub}"


